package ldh.im.web.controller;

import java.util.Date;
import java.util.Objects;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import ldh.common.PageResult;
import ldh.common.json.JsonViewFactory;
import ldh.common.mvc.DateEditor;
import ldh.im.business.constant.ArticleStatus;
import ldh.im.business.constant.ArticleType;
import ldh.im.business.pojo.CmsArticle;
import ldh.im.business.pojo.where.CmsArticleWhere;
import ldh.im.business.service.CmsArticleService;
import ldh.im.web.service.TenantService;
import ldh.im.web.util.UserUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
* @author: ldh
* @date: 2021-07-04 17:02:34
*/
@Controller
@RequestMapping("cmsArticle")
public class CmsArticleController extends BaseController {

	@Resource(name = "cmsArticleService")
	private CmsArticleService cmsArticleService;

	/**
	 * 字符串转化成时间
	 */
	@InitBinder  
	protected void initBinder(HttpServletRequest request,  
	                              ServletRequestDataBinder binder) throws Exception {  
	    //对于需要转换为Date类型的属性，使用DateEditor进行处理  
	    binder.registerCustomEditor(Date.class, new DateEditor());  
	} 

	@RequiresPermissions("cmsArticle:save")
    @RequestMapping(method = RequestMethod.POST, value = "/save")
    public String save(@ModelAttribute CmsArticle cmsArticle, HttpSession httpSession) throws Exception {
    	Objects.requireNonNull(cmsArticle);
		cmsArticle.setScore(0);
		cmsArticle.setCreateTime(new Date());
		cmsArticle.setUpdateTime(new Date());
		cmsArticle.setUserId(UserUtil.getLoginUser(httpSession).getId());
    	if (cmsArticle.getId() == null) {
			cmsArticleService.insert(cmsArticle);
    	} else {
			cmsArticleService.updateById(cmsArticle);
    	}
    	return "redirect:/cmsArticle/list";
    }

	@RequiresPermissions("cmsArticle:save")
    @RequestMapping(method = RequestMethod.POST, value = "/save/json")
    @ResponseBody
    public String saveJson(@ModelAttribute CmsArticleWhere cmsArticle) throws Exception {
    	Objects.requireNonNull(cmsArticle);
    	if (cmsArticle.getId() == null) {
			cmsArticleService.insert(cmsArticle);
		} else {
			cmsArticleService.updateById(cmsArticle);
		}
        return JsonViewFactory.create()
				.toJson();
    }

	@RequiresPermissions("cmsArticle:edit")
	@RequestMapping(method = RequestMethod.GET, value = "/toEdit/{id}")
    public String toEdit(@PathVariable Long id, Model model) throws Exception {
		CmsArticle cmsArticle = cmsArticleService.getById(id);

		model.addAttribute("tenants", getAllTenants());
    	model.addAttribute("cmsArticle", cmsArticle);
		model.addAttribute("active", "cmsArticle");
		model.addAttribute("statusValues", ArticleStatus.values());
		model.addAttribute("articleTypes", ArticleType.values());
        return "cmsArticle/cmsArticleEdit";
    }

	@RequiresPermissions("cmsArticle:toAdd")
	@RequestMapping(method = RequestMethod.GET, value = "/toAdd")
    public String toAdd(Model model) throws Exception {
		model.addAttribute("tenants", getAllTenants());
		model.addAttribute("active", "cmsArticle");
		model.addAttribute("statusValues", ArticleStatus.values());
		model.addAttribute("articleTypes", ArticleType.values());
        return "cmsArticle/cmsArticleEdit";
    }

	@RequiresPermissions("cmsArticle:view")
	@RequestMapping(method = RequestMethod.GET, value = "/view/{id}")
    public String view(@PathVariable Long id, Model model) throws Exception {
		Objects.requireNonNull(id);
    	CmsArticle cmsArticle = cmsArticleService.getById(id);
    	cmsArticle.setTenant(tenantService.getById(cmsArticle.getTenantId()));
    	model.addAttribute("cmsArticle", cmsArticle);
		model.addAttribute("active", "cmsArticle");
        return "cmsArticle/cmsArticleView";
    }

	@RequiresPermissions("cmsArticle:edit")
	@ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/view/json/{id}")
    public String viewJson(@PathVariable Long id, Model model) throws Exception {
		Objects.requireNonNull(id);
    	CmsArticle cmsArticle = cmsArticleService.getById(id);
    	return JsonViewFactory.create()
				.setDateFormat("yyyy-MM-dd hh:mm:ss")
				.put("data", cmsArticle)
				.toJson();
    }

	@RequiresPermissions("cmsArticle:list")
	@RequestMapping(method = RequestMethod.GET, value = "/list")
	public String list(@ModelAttribute CmsArticleWhere cmsArticleWhere, Model model) {
		cmsArticleWhere.setOrder("id desc");
		PageResult<CmsArticle> cmsArticles = cmsArticleService.findByCmsArticleWhere(cmsArticleWhere);
		cmsArticles.getBeans().forEach(a->a.setTenant(tenantService.getById(a.getTenantId())));
		model.addAttribute("tenants", getAllTenants());
		model.addAttribute("cmsArticles", cmsArticles);
		model.addAttribute("active", "cmsArticle");
		model.addAttribute("cmsArticle", cmsArticleWhere);
		model.addAttribute("statusValues", ArticleStatus.values());
		model.addAttribute("articleTypes", ArticleType.values());
		return "cmsArticle/cmsArticleList";
	}

	@RequiresPermissions("cmsArticle:edit")
	@RequestMapping(method = RequestMethod.GET, value = "/list/json")
	@ResponseBody
	public String listJson(@ModelAttribute CmsArticleWhere cmsArticleWhere, Model model) {
		cmsArticleWhere.setOrder("id desc");
		PageResult<CmsArticle> cmsArticles = cmsArticleService.findByCmsArticleWhere(cmsArticleWhere);
		return JsonViewFactory.create()
				.setDateFormat("yyyy-MM-dd hh:mm:ss")
				.put("data", cmsArticles)
				.toJson();
	}
	
	@RequiresPermissions("cmsArticle:delete")
	@RequestMapping(method = RequestMethod.GET, value = "/deleteById/json/{id}")
	@ResponseBody
	public String deleteJsonById(@PathVariable("id")Long id) {
		CmsArticle cmsArticle = cmsArticleService.getById(id);
        Objects.requireNonNull(cmsArticle);
		cmsArticleService.deleteById(id);
        return JsonViewFactory.create()
				.toJson();
        }

	
}